package com.dhl.fin.api.controller;

import com.dhl.fin.api.common.controller.CommonController;
import com.dhl.fin.api.common.dto.ApiResponse;
import com.dhl.fin.api.common.dto.QueryDto;
import com.dhl.fin.api.common.exception.BusinessException;
import com.dhl.fin.api.common.util.MapUtil;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.common.util.WebUtil;
import com.dhl.fin.api.domain.Family;
import com.dhl.fin.api.dto.TagGroupDTO;
import com.dhl.fin.api.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author CuiJianbo
 * @since 2025/3/16
 */
@RestController
@RequestMapping("family")
public class FamilyController extends CommonController<Family> {

    @Autowired
    private FamilyService familyService;

    @Autowired
    private TagService tagService;

    @Autowired
    private BankAccountService bankAccountService;

    @Autowired
    private BankStatementService bankStatementService;

    @Autowired
    private ConsultService consultService;

    @Autowired
    private FamilyConfigService familyConfigService;


//    @ResponseBody
//    @GetMapping("getunicode")
//    public ApiResponse getFamilyUnicode() throws Exception {
//
//        String familyUnicode = familyService.getFamilyUnicode();
//
//        return ApiResponse.success(familyUnicode);
//
//    }

    @ResponseBody
    @GetMapping("unbindWx")
    public ApiResponse unbindWx(Long id) throws Exception {

        String unicode = familyService.unbindWx(id);

        return ApiResponse.success(unicode);
    }

    @ResponseBody
    @GetMapping("unbindOwnerWx")
    public ApiResponse unbindOwnerWx(Long id, String password, String faUnicode) throws Exception {

        try {
            //校验密码
            familyConfigService.checkPasswordRight(faUnicode, password);
        } catch (BusinessException e) {
            return ApiResponse.success("密码错误");
        }

        //解绑微信账号，返回新的家庭unicode
        String unicode = familyService.unbindWx(id);

        return ApiResponse.success(unicode);

    }

    @ResponseBody
    @GetMapping("info")
    public ApiResponse familyInfo() throws Exception {
        Family family = familyService.get(QueryDto.builder()
                .available()
                .addWhere("wx_account_code='" + WebUtil.getLoginUser().getUuid() + "'")
                .build());

        if (ObjectUtil.isNull(family)) {
            return ApiResponse.error("请添加家庭成员");
        } else {
            List<TagGroupDTO> tags = tagService.selectTagGroup(family.getUnicode());

            //计算定期存钱的每年利息
            List<Map> yearRate = bankStatementService.calculateInterest(0.0, family.getUnicode());

            //获取活期存款年化利率
            String timeYearRate = familyConfigService.getTimeYearRate();

            //获取借出年化利率
            String lendYearRate = familyConfigService.getLendYearRate();

            //获取借出逾期年化率
            String lendPenaltyYearRate = familyConfigService.getLendPenaltyYearRate();

            //判断是否要提示用户激活卡片
            boolean tipActiveAccount = bankAccountService.checkIsAllNotActive();

            return ApiResponse.success(MapUtil.builder()
                    .add("family", family)
                    .add("tags", tags)
                    .add("yearRate", yearRate)
                    .add("timeYearRate", timeYearRate)
                    .add("lendYearRate", lendYearRate)
                    .add("lendPenaltyYearRate", lendPenaltyYearRate)
                    .add("tipActiveAccount", tipActiveAccount)
                    .build());
        }
    }


    @ResponseBody
    @GetMapping("countChild")
    public ApiResponse countChild(String unicode) throws Exception {
        List<Family> families = familyService.select(QueryDto.builder()
                .available()
                .addWhere("unicode = '" + unicode + "'")
                .addWhere("member_alias not in ('爸爸','妈妈') ")
                .build());

        return ApiResponse.success(families.size());
    }

    @ResponseBody
    @GetMapping("checkfamily")
    public ApiResponse checkFamily() throws Exception {

        //判断是否有即将到期的卡片
        Boolean hasExpireCard = bankAccountService.checkWillExpireCard();

        //判断是否已经提交过咨询
        Boolean hasConsult = consultService.hasConsult();

        //判断是否有已经回复
        Boolean hasReplyConsult = consultService.hasReplyConsult();

        //判断是否已填写父母信息
        Boolean hasParents = familyService.checkHasParents();

        //判断是否已填加小孩信息
        Boolean hasChild = familyService.checkHasChild();

        return ApiResponse.success(MapUtil.builder()
                .add("hasExpireCard", hasExpireCard)
                .add("hasConsult", hasConsult)
                .add("hasReplyConsult", hasReplyConsult)
                .add("hasParents", hasParents)
                .add("hasChild", hasChild)
                .build());
    }

    @ResponseBody
    @GetMapping("deleteChild/{id}")
    public ApiResponse deleteChild(@PathVariable Long id, String password, String faUnicode) throws Exception {

        try {
            //校验密码
            familyConfigService.checkPasswordRight(faUnicode, password);
        } catch (BusinessException e) {
            return ApiResponse.success("密码错误");
        }

        Family family = familyService.get(id, "bankAccount");

        //删除人员
        familyService.deleteById(id);

        if (ObjectUtil.notNull(family.getBankAccount())) {
            //删除微行卡
            bankAccountService.deleteById(family.getBankAccount().getId());
        }

        return ApiResponse.success();
    }


    @ResponseBody
    @GetMapping("members")
    public ApiResponse queryAllFamilyMember() throws Exception {
        Map data = familyService.queryFamilyMember();
        return ApiResponse.success(data);
    }


}



